import { createRouter, createWebHashHistory } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';
import Layout from '../layout/Layout.vue';

// 路由配置
const routes: Array<RouteRecordRaw> = [
  // 登录页面
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/LoginView.vue'),
    meta: { 
      title: '登录'
    }
  },
  // 主应用布局
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    children: [
      // 1. 首页
      {
        path: '',
        name: 'Home',
        component: () => import('../views/HomeView.vue'),
        meta: { title: '首页' }
      },
      // 2. 案件管理
      {
        path: '/review-list',
        name: 'ReviewList',
        component: () => import('../views/ReviewListView.vue'),
        meta: { title: '案件审核' }
      },
      {
        path: '/record-case-list',
        name: 'RecordCaseList',
        component: () => import('../views/RecordCaseListView.vue'),
        meta: { title: '录案管理' }
      },
      {
        path: '/one-click-case-list',
        name: 'OneClickCaseList',
        component: () => import('../views/OneClickCaseListView.vue'),
        meta: { title: '一键委案案件列表' }
      },
      {
        path: '/one-click-case-detail/:id',
        name: 'OneClickCaseDetail',
        component: () => import('../views/OneClickCaseDetailView.vue'),
        meta: { title: '一键委案案件详情' }
      },
      {
        path: '/entrust-list',
        name: 'EntrustList',
        component: () => import('../views/EntrustListView.vue'),
        meta: { title: '委托案件' }
      },
      {
        path: '/case-distribution-list',
        name: 'CaseDistributionList',
        component: () => import('../views/CaseDistributionListView.vue'),
        meta: { title: '案件分案' }
      },
      {
        path: '/withdrawal-case-list',
        name: 'WithdrawalCaseList',
        component: () => import('../views/WithdrawalCaseView.vue'),
        meta: { title: '撤委案管理' }
      },
      {
        path: '/case-detail/:id',
        name: 'CaseDetail',
        component: () => import('../views/CaseDetailView.vue'),
        meta: { title: '案件详情' }
      },
      // 3. 系统工具
      {
        path: '/excel-split/citic',
        name: 'ExcelSplitCitic',
        component: () => import('../views/ExcelSplit/Citic.vue'),
        meta: { title: '中信银行分表' }
      },
      {
        path: '/excel-split/pingan',
        name: 'ExcelSplitPingAn',
        component: () => import('../views/ExcelSplit/PingAn.vue'),
        meta: { title: '平安银行分表' }
      },
      {
        path: '/excel-split/yingtan',
        name: 'ExcelSplitYingtan',
        component: () => import('../views/ExcelSplit/YingTan.vue'),
        meta: { title: '鹰潭制表' }
      },
      {
        path: '/scheduled-task',
        name: 'ScheduledTask',
        component: () => import('../views/ScheduledTaskView.vue'),
        meta: { title: '定时任务执行' }
      },
      {
        path: '/profile',
        name: 'Profile',
        component: () => import('../views/ProfileView.vue'),
        meta: { title: '个人中心' }
      }
    ]
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

// 全局导航守卫
router.beforeEach(async (to, _from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title as string;
  }

  // 直接放行，不做任何权限判断
  next();
});

export default router;